package com.td.client.states;

import java.util.Iterator;

import mdes.slick.sui.Display;
import mdes.slick.sui.Label;
import mdes.slick.sui.Point;
import mdes.slick.sui.Window;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;

import com.td.client.ClientWindow;
import com.td.core.communication.obj.PlayerInfo;
import com.td.core.communication.obj.PreGameInfo;


//TODO
/*
 * GUI jest ekstremalnie proste -> w swoim czasie zrobi sie je przyjemne dla oka
 * 
 * 
 */
public class WaitState extends State{
	
	private Display display;
	
	private Label time;
	private Label connectedPlayers;
	
	public WaitState(ClientWindow clientWindow, GameContainer gameContainer) {
		super(clientWindow, gameContainer);
		buildGUI();
	}

	@Override
	public void render(GameContainer gc, Graphics g) throws SlickException {
		time.setText("Game starts in: " + mainGame.getPreGameInfo().getTimeToGame());
		time.updateAppearance();
		
		Iterator<PlayerInfo> it = mainGame.getPreGameInfo().getPlayersConnected().iterator();
		String players = "Connected players:\n";
		synchronized (PreGameInfo.class) {
			while(it.hasNext()) {
				PlayerInfo pi = it.next();
				players += pi.getName()+"\n";
			}	
		}
		connectedPlayers.setText(players);
		connectedPlayers.updateAppearance();
		
		display.render(gc, g);
	}

	@Override
	public void update(GameContainer gc, int delta) {
		display.update(gc, delta);
	}
	
	private void buildGUI() {
		
		display = new Display(gameContainer);
		Window window = new Window();
		window.setOpaque(true);
		window.setBackground(Color.lightGray);
		
		window.setSize(800, 700);
		window.setLocation(0, 0);
		window.setAlwaysOnTop(true);
		display.add(window);
		
		time = new Label();
		time.setLocation(new Point(100, 100));
		time.setSize(100, 100);
		
		connectedPlayers = new Label();
		connectedPlayers.setLocation(new Point(10, 200));
		connectedPlayers.setSize(700, 100);
		
		window.add(time);
		window.add(connectedPlayers);		
	}
}